﻿#include <iostream>
#include<string>
#include <vector>
using namespace std;
/* 对象行为型模式
1.意图
提供一种方法顺序访问一个聚合对象中各个元素，而又不需暴露该对象的内部表示
2.别名
游标（Cursor)
3.适用性
a.访问一个聚合对象的内容而无需暴露它的内部表示
b.支持对聚合对象的多种遍历
c.为遍历不同的聚合结构提供一个统一的接口（即支持多态迭代）
*/
template <class T,class U>

class Iterator
{
public:
	typedef typename vector<T>::iterator iter_type;
	Iterator(U* data,bool reverse=false):m_data(data),iter(data->m_data.begin()){}
	void Begin()
	{
		iter = m_data->m_data.begin();
	}
	void Next()
	{
		iter++;
	}
	bool End()
	{
		return (iter == m_data->m_data.end());
	}
	iter_type Current()
	{
		return iter;
	}
private:
	U* m_data;
	iter_type iter;
};

template <class T>
class Container
{
	friend class Iterator<T, Container>;
public:
	void Add(T data)
	{
		m_data.push_back(data);
	}
	Iterator<T, Container>* CreateIterator()
	{
		return new Iterator<T, Container>(this);
	}
protected:
	vector<T> m_data;
};
class Data
{
public:
	Data(int data):m_data(data){}
	int GetData()
	{
		return m_data;
	}
	void SetData(int data)
	{
		m_data = data;
	}
protected:
	int m_data;
};
/*
int main()
{
	Container<int> intData;
	for (int i = 0; i < 10; i++)
	{
		intData.Add(i);
	}
	Iterator<int, Container<int>>* iter = intData.CreateIterator();
	for (iter->Begin(); !iter->End(); iter->Next())
	{
		cout << *iter->Current() << "  ";
	}

	Container<Data> testData;
	testData.Add(Data(123));
	testData.Add(Data(456));
	testData.Add(Data(789));
	Iterator<Data, Container<Data>>* iter1 = testData.CreateIterator();
	for (iter1->Begin(); !iter1->End(); iter1->Next())
	{
		cout << iter1->Current()->GetData() << "  ";
	}
	return 0;
}
*/